package com.slipper.system.organization;

import java.util.List;

import javax.servlet.http.HttpServletResponse;

import com.slipper.common.bo.Result;
import javax.annotation.Resource;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.slipper.system.api.organization.bo.OrganizationBO;
import com.slipper.system.api.organization.service.IOrganizationSv;
import com.slipper.system.common.Constants;
import com.slipper.common.permit.PermitAll;
import com.slipper.common.tree.TreeInfo;
import com.slipper.common.tree.TreeUtil;
import com.slipper.common.util.AttachUtil;
import com.slipper.common.util.ResultUtil;

/**
 * 组织机构信息表
 *
 * @date 2020-03-26 14:59:54
 */
@RestController
@RequestMapping("/organization")
public class OrganizationController {
    @Resource
    private IOrganizationSv organizationSv;

    @PermitAll
    @PostMapping("/queryBy")
    public String queryBy(@RequestBody OrganizationBO bo) {
        return ResultUtil.toJson(organizationSv.queryBy(bo.getId()));
    }

    @PermitAll
    @PostMapping("/queryTree")
    public String queryTree(@RequestBody OrganizationBO bo) {
        bo.setValid(Constants.VALID);
        Result<List<OrganizationBO>> organizationList = organizationSv.queryList(bo);
        OrganizationBO organization = new OrganizationBO();
        organization.setId(Constants.ROOT_ID);
        organization.setShortName("部门树");
        return this.buildTree(organization, organizationList.getData());
    }

    @PermitAll
    @PostMapping("/queryUnitTree")
    public String queryUnitTree(@RequestBody OrganizationBO bo) {
        bo.setValid(Constants.VALID);
        bo.setType(OrganizationBO.TYPE_UNIT);
        Result<List<OrganizationBO>> organizationList = organizationSv.queryList(bo);
        Result<OrganizationBO> manOrganization = organizationSv.queryBy(Constants.getCurrentUser().getUnitId());
        return this.buildTree(manOrganization.getData(), organizationList.getData());
    }

    private String buildTree(OrganizationBO organization, List<OrganizationBO> organizationList) {
        TreeInfo treeInfo = new TreeInfo(organization.getShortName());
        treeInfo.setRootId(organization.getId());
        treeInfo.setIdField("id");
        treeInfo.setPidField("parentId");
        treeInfo.setTextField("shortName");
        treeInfo.setSortField("ordinal");
        treeInfo.setData(organization);
        return ResultUtil.toJson(TreeUtil.buildTree(treeInfo, organizationList,"asc"));
    }

    @PermitAll
    @PostMapping("/queryPage")
    public String queryPage(@RequestBody OrganizationBO bo) {
        if (!Constants.getCurrentUser().isSuperAdmin()) {
            if (bo.getParentId() == null || "".equals(bo.getParentId())) {
                bo.setParentId(Constants.getCurrentUser().getUnitId());
            }
        }
        return ResultUtil.toJson(organizationSv.queryPage(bo));
    }

    @PermitAll
    @PostMapping("/queryPageValid")
    public String queryPageValid(@RequestBody OrganizationBO bo) {
        if (bo.getParentId() == null || "".equals(bo.getParentId())) {
            bo.setParentId(Constants.getCurrentUser().getUnitId());
        }
        bo.setValid(Constants.VALID);
        return ResultUtil.toJson(organizationSv.queryPage(bo));
    }


    @PostMapping("/save")
    public String save(@RequestBody OrganizationBO bo) {
        return ResultUtil.toJson(organizationSv.save(bo));
    }

    @PostMapping("/delete")
    public String delete(@RequestBody OrganizationBO bo) {
        String[] ids = null;
        if (bo.getIds() != null) {
            ids = bo.getIds();
        } else if (bo.getId() != null) {
            ids = new String[] { bo.getId() };
        }
        if (ids == null) {
            return ResultUtil.toErrorJson("参数不正确！");
        }

        return ResultUtil.toJson(organizationSv.delete(bo));
    }

    @PostMapping("/downloadTpl")
    public void downloadTpl(HttpServletResponse response) {
        AttachUtil.downloadTemplate(response, "OrganizationTpl.xls", "部门信息导入模板.xls");
    }

    @PostMapping("/updateValid/{valid}")
    public String updateValid(@PathVariable("valid") String valid, @RequestBody OrganizationBO bo) {
        bo.setValid(valid);
        return ResultUtil.toJson(organizationSv.updateValid(bo));
    }
}
